/*
** main.c for Main in /home/vriely/Work/Mathematics/205
**
** Made by Samuel Vrielynck
** Login   <vriely_s@epitech.net>
**
** Started on  Mon Apr 23 12:26:44 2012 Samuel Vrielynck
** Last update Tue Apr 24 09:11:01 2012 pierrick druart
*/

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <math.h>

static double		repartition(double x)
{
  return exp( - (x * x) / 2);
}

static double		simpson_law(double val)
{
  int			i;
  int			n = 20;
  double		a = -10;
  double		h;
  double		x;
  double		y;
  double		z;
  double		res;

  y = 0;
  z = 0;
  h = (val - a) / n;
  x = repartition(a) + repartition(val);
  for (i = 1; i <= n - 1; i++)
    if (!(i % 2))
      z = z + repartition(a + i * h);
    else
      y = y + repartition(a + i * h);
  res = (x + 4 * y + 2 * z) * h / 3;
  return (res);
}

int		main(int argc, const char *argv[])
{
  double	res;

  if (argc != 3)
    {
      fprintf(stderr, "Usage : %s [A] [B]\n", argv[0]);
      return (EXIT_FAILURE);
    }
  else
    {
    res = (1 / sqrt(2 * M_PI) * simpson_law(atof(argv[2]))) -
      (1 / sqrt(2 * M_PI) * simpson_law(atof(argv[1])));
    printf("P(%s <= Z <= %s) = %0.3f\n", argv[1], argv[2], res);
    }
  return (EXIT_SUCCESS);
}
